home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-11-19 | 8.0 KB | 266 lines | [TEXT/MPS ] |
- /*
- File: SampleLibrary.cp
-
- Contains: Implementation of the CPlusSampleLibrary shared library.
-
- Copyright: © 1993-1994 by Apple Computer, Inc., all rights reserved.
-
- */
-
-
- #include <GlobalNew.h>
- #include <LibraryManager.h>
- #include <LibraryManagerClasses.h>
- #include <LibraryManagerUtilities.h>
-
- #include <String.h>
- #include <Events.h>
- #include <QuickDraw.h>
- #include <Resources.h>
- #include <Menus.h>
- #include <Windows.h>
- #include <StdIO.h>
-
- #include "SampleLibrary.h"
-
- //————————————————————————————————————————————————————————————————————————————————————
- // TTrafficLight
- //
- // TrafficLight constructor, allocates window record from client's pool, and ini-
- // tialize our light rectangles.
- //————————————————————————————————————————————————————————————————————————————————————
-
- TTrafficLight::TTrafficLight()
- {
- long savedrefnum = -1;
- Ptr wstorage = nil;
-
- VOLATILE(wstorage); // use this if variable is being modified within
- VOLATILE(savedrefnum); // TRY and accessed in the CATCH_ALL.
-
- fWindow = nil;
-
- Trace("TTrafficLight Constructor\n");// send the output to trace monitor's window
-
- TRY
- fLightState = false;
-
- wstorage = (Ptr)new WindowRecord;// allocate our storage
-
- FailNULL( wstorage, ErrorCode(), "sorry not enought memory in pool");
-
- // include the library's file in resource chain to obtain our menus & windows
-
- Fail(GetLocalLibraryFile()->Preflight(savedrefnum), "Failed to preflight");
-
- fWindow = GetNewWindow( rWindow, wstorage, (WindowPtr) -1 );
-
- FailNULL( fWindow, ResError(), "sorry unable to create a window");
-
- TTrafficLight::GoGetRect(rStopRect, &fStopRect); // get rectangle for Stop light
- TTrafficLight::GoGetRect(rGoRect, &fGoRect); // get rectangle for Go light
-
- // Any MENU resources in our shared library is appended to our application
-
- MenuHandle themenu = GetMenu( mLight ); // get our traffic light menu
- InsertMenu( themenu, 0 ); // append to the application's menu
-
- DrawMenuBar(); // go ahead and update the menu bar
- CATCH_ALL // oh no! something failed lets cleanup
- delete wstorage; // remove the storage
- delete fWindow; // free the pool
- ENDTRY
-
- if (savedrefnum != -1)
- GetLocalLibraryFile()->Postflight(savedrefnum); // restore the resource chain
- }
-
- //————————————————————————————————————————————————————————————————————————————————————
- // ~TTrafficLight
- //
- // our traffic light destructor, delete the window, and unregister the object for
- // inspector
- //————————————————————————————————————————————————————————————————————————————————————
-
- TTrafficLight::~TTrafficLight()
- {
- Trace("TTrafficLight Destructor\n");// tell Trace Monitor where we are
-
- CloseWindow( fWindow ); // close the window
- delete fWindow; // we are done, release the memory
- }
-
- //————————————————————————————————————————————————————————————————————————————————————
- // IsValid
- //
- // returns true if the object was initialized properly after it was created. In our
- // case if window has been allocated, we return true.
- //————————————————————————————————————————————————————————————————————————————————————
-
- Boolean TTrafficLight::IsValid() const
- {
- return( fWindow != nil ); // return true if window has been allocated
- }
-
- //————————————————————————————————————————————————————————————————————————————————————
- // GetLightState
- //
- // return current state of traffic light
- //————————————————————————————————————————————————————————————————————————————————————
-
- Boolean TTrafficLight::GetLightState() const
- {
- Trace("TTrafficLight::GetLightState\n"); // tell Trace Monitor where we are
- return fLightState; // return the state of Traffic Light
- }
-
- //————————————————————————————————————————————————————————————————————————————————————
- // SetLightState
- //
- // set the light to the new state
- //————————————————————————————————————————————————————————————————————————————————————
-
- void TTrafficLight::SetLightState( Boolean newState )
- {
- Trace("TTrafficLight::SetLightState\n"); // tell Trace Monitor what we are doing
- fLightState = newState; // set the new state
-
- SetPort(fWindow); // make sure we are at the right port before
- InvalRect(&fWindow->portRect); // invalidating the update region
- }
-
- //————————————————————————————————————————————————————————————————————————————————————
- // DrawLight
- //
- // draw the actual traffic light
- //————————————————————————————————————————————————————————————————————————————————————
-
- void TTrafficLight::DrawLight()
- {
- GrafPtr oldport;
-
- Trace("TTrafficLight::DrawLight\n"); // tell the Trace Monitor what we are doing
-
- if( fWindow ) {
-
- GetPort( &oldport ); // save the old port
- SetPort( fWindow ); // set the new port to our object's port
-
- EraseRect(&fWindow->portRect); // clear out any garbage that may linger
- if ( fLightState ) // draw a red (or white) stop light
- ForeColor(redColor);
- else
- ForeColor(whiteColor);
-
- PenSize( 2, 2 );
- PaintOval(&fStopRect);
- ForeColor(blackColor);
- FrameOval(&fStopRect);
- if ( ! fLightState ) // draw a green (or white) go light
- ForeColor(greenColor);
- else
- ForeColor(whiteColor);
- PaintOval(&fGoRect);
- ForeColor(blackColor);
- FrameOval(&fGoRect);
- PenSize( 1, 1 );
-
- SetPort( oldport ); // restore the grafpointer
- }
- }
-
- //————————————————————————————————————————————————————————————————————————————————————
- // AdjustMenus
- //
- // Enable and disable traffic light's menus based on the current state.
- //————————————————————————————————————————————————————————————————————————————————————
-
- void TTrafficLight::AdjustMenus( Boolean active )
- {
- long savedrefnum;
- MenuHandle menu;
- OSErr err;
-
- Trace("TTrafficLight::AdjustMenus\n");// tell the Trace Monitor where we are
-
- // include the library in resource chain so we can allocate our menu resources
-
- if( err = GetLocalLibraryFile()->Preflight( savedrefnum ) ) {
- Trace("Preflight failed err=%d\n", err);// let Trace Monitor know what failed
- return;
- }
-
- if( menu = GetMenuHandle(mLight) ) { // get the handle to traffic light menu
-
- if ( active ) { // do we need to enable them?
- EnableItem(menu, iStop);
- EnableItem(menu, iGo);
- } else {
- DisableItem(menu, iStop);
- DisableItem(menu, iGo);
- }
-
- CheckItem(menu, iStop, fLightState); // we can also determine check/uncheck state, too
- CheckItem(menu, iGo, ! fLightState);
- }
- else
- Trace("GetMenuHandle returned NIL\n"); // let Trace Monitor know we failed
-
- // restore the resource chain to it previous state
-
- err = GetLocalLibraryFile()->Postflight( savedrefnum );
- }
-
- //————————————————————————————————————————————————————————————————————————————————————
- // DoMenuCommand
- //
- // Enable and disable menus based on the current state of traffic light.
- //————————————————————————————————————————————————————————————————————————————————————
-
- void TTrafficLight::DoMenuCommand( short menuItem )
- {
- long savedrefnum;
- OSErr err;
-
- Trace("TTrafficLight::DoMenuCommand\n"); // tell Trace Monitor what we are doing
- // include the library's file in resource chain
- if( err = GetLocalLibraryFile()->Preflight( savedrefnum ) ) {
- Trace("Preflight failed err=%d\n", err);// let Trace Monitor know what failed
- return;
- }
-
- switch ( menuItem ) { // what menu item selected?
- case iStop:
- SetLightState( true ); // set and update
- break;
- case iGo:
- SetLightState( false ); // set and update
- break;
- }
- // restore the resource change
- err = GetLocalLibraryFile()->Postflight(savedrefnum);
- }
-
- //————————————————————————————————————————————————————————————————————————————————————
- // GoGetRect
- //
- // loads the global rectrangles that are used for drawing the traffic lights.
- //————————————————————————————————————————————————————————————————————————————————————
-
- Boolean TTrafficLight::GoGetRect( short rectID, Rect *theRect)
- {
- Handle resource;
-
- TRY
- resource = GetResource('RECT', rectID); // get 'RECT' resource from library file
- FailNULL( resource, ResError(), "'RECT' resource is missing" );
- *theRect = **((Rect**) resource);
- CATCH_ALL
- DebugStr("\pFailed to get 'RECT' resource");// inform the user we failed
- SetRect( theRect, 10, 10, 60, 60 );
- return false;
- ENDTRY
-
- return true;
- }
-